// 1. React核心库
import React, { useState, useEffect } from 'react';

// 2. UI组件库 - 按字母顺序
import { Button, Form, Input, Select, Space, message } from 'antd';

// 5. 工具库
 

// 7. 项目内部模块
import { apiClients } from '@/services/apiClient';
import { autoLoginInDev } from '@/utils/autoLogin';

// 8. 生成的类型定义 (必须使用，禁止手工定义)
import type { UserFavoriteRequest, AIConversation } from '@/services/generated';

const { TextArea } = Input;

interface FavoriteFormProps {
  initialValues?: Partial<UserFavoriteRequest>;
  onSubmit: (values: UserFavoriteRequest) => Promise<void>;
  onCancel: () => void;
  loading?: boolean;
}

const FavoriteForm: React.FC<FavoriteFormProps> = ({
  initialValues,
  onSubmit,
  onCancel,
  loading = false,
}) => {
  const [form] = Form.useForm();
  const [conversations, setConversations] = useState<AIConversation[]>([]);

  // 获取对话列表
  const  fetchConversations = 
    async () => {
      try {
        await autoLoginInDev();
        const response = await apiClients.aiChat.aiChatConversationsList({
          pageSize: 100,
        });
        setConversations(response.data?.items || []);
      } catch (error) {
        message.error('获取对话列表失败');
      }
    } 
  const handleSubmit = async () => {
    try {
      const values = await form.validateFields();
      await onSubmit(values);
    } catch (error) {
      // 表单验证失败
    }
  };

  return (
    <Form
      form={form}
      layout="vertical"
      initialValues={initialValues}
    >
      <Form.Item
        label="收藏标题"
        name="title"
        rules={[
          { required: true, message: '请输入收藏标题' },
          { max: 200, message: '标题长度不能超过200个字符' },
        ]}
      >
        <Input placeholder="请输入收藏标题" />
      </Form.Item>

      <Form.Item
        label="关联对话"
        name="conversation"
        rules={[{ required: true, message: '请选择关联的对话' }]}
      >
        <Select
          placeholder="请选择要收藏的对话"
          showSearch
          filterOption={(input, option) =>
            (option?.label ?? '').toLowerCase().includes(input.toLowerCase())
          }
          options={conversations.map(conv => ({
            value: conv.id,
            label: conv.title,
          }))}
        />
      </Form.Item>

      <Form.Item
        label="备注"
        name="note"
        rules={[{ max: 500, message: '备注长度不能超过500个字符' }]}
      >
        <TextArea
          rows={4}
          placeholder="请输入备注信息（可选）"
        />
      </Form.Item>

      <Form.Item>
        <Space>
          <Button type="primary" loading={loading} onClick={handleSubmit}>
            保存
          </Button>
          <Button onClick={onCancel}>
            取消
          </Button>
        </Space>
      </Form.Item>
    </Form>
  );
};

export default FavoriteForm;